package com.flying.invest.assets.service;

import java.util.Date;
import java.util.List;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.annotation.Transaction;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.context.ServiceContext;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.security.Principal;
import com.flying.framework.service.AbstractService;
import com.flying.invest.assets.model.Assets;
import com.flying.invest.assets.model.AssetsPriceSpider;
import com.flying.invest.assets.spider.IPriceSpider;
import com.flying.invest.assets.spider.SpiderPrice;

@Service(value="AssetsPriceSpiderService", desc="")
public class AssetsPriceSpiderService extends AbstractService{
    private AssetsService assetsService;
    public void setAssetsService(AssetsService s) {
    	this.assetsService = s;
    }
	@MethodInfo("新增")
	@DaoCreate(entity=AssetsPriceSpider.ENTITY_NAME)
	public AssetsPriceSpider create(
			@Param(value=AssetsPriceSpider.USER_ID, required=false,	desc="所属用户") Long user_id,
			@Param(value=AssetsPriceSpider.ASSETS_ID, required=true,	desc="投资品种ID") Long assets_id,
			@Param(value=AssetsPriceSpider.URL, required=false,	desc="价格页面") String url,
			@Param(value=AssetsPriceSpider.PARAMS, required=false,	desc="价格页面参数") String params,
			@Param(value=AssetsPriceSpider.MODULE, required=false,	desc="解析模块") String module,
			@Param(value=AssetsPriceSpider.SERVICE, required=false,	desc="解析服务") String service,
			@Param(value=AssetsPriceSpider.INTERVALS, required=false,	desc="时间间隔") Long intervals,
			@Param(value=AssetsPriceSpider.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=AssetsPriceSpider.CREATE_TIME, required=false,	desc="创建时间") Date create_time
		
		) throws Exception {
		Principal p = ServiceContext.getContext().getPrincipal();
		if(user_id == null) user_id = Long.parseLong(p.getId());
		if(module == null) module = "invest";
		return ModelFactory.create(AssetsPriceSpider.class, 
				AssetsPriceSpider.USER_ID, user_id,
				AssetsPriceSpider.MODULE, module, 
				AssetsPriceSpider.SERVICE, service,
				AssetsPriceSpider.CREATE_TIME, new Date());
	}
	
	@MethodInfo("新增")
	public AssetsPriceSpider create2(
			@Param(value=AssetsPriceSpider.ASSETS_ID, required=true,	desc="投资品种ID") Long assets_id,
			@Param(value=AssetsPriceSpider.SERVICE, required=false,	desc="解析服务") String service
		
		) throws Exception {
		return this.create(null, assets_id, null, null, null, service, null, null, null);
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=AssetsPriceSpider.ENTITY_NAME)
	public ActionResult update(
			@Param(value=AssetsPriceSpider.SPIDER_ID, required=true,	desc="SPIDER ID") Long spider_id,
			@Param(value=AssetsPriceSpider.ASSETS_ID, required=true,	desc="投资品种ID") Long assets_id,
			@Param(value=AssetsPriceSpider.URL, required=false,	desc="价格页面") String url,
			@Param(value=AssetsPriceSpider.PARAMS, required=false,	desc="价格页面参数") String params,
			@Param(value=AssetsPriceSpider.MODULE, required=false,	desc="解析模块") String module,
			@Param(value=AssetsPriceSpider.SERVICE, required=false,	desc="解析服务") String service,
			@Param(value=AssetsPriceSpider.INTERVALS, required=false,	desc="时间间隔") Long intervals,
			@Param(value=AssetsPriceSpider.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=AssetsPriceSpider.NEXT_TIME, required=false,	desc="下次执行时间") Date next_time
			
		) throws Exception {
		
		return null;
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=AssetsPriceSpider.ENTITY_NAME)
	public ActionResult update2(
			@Param(value=AssetsPriceSpider.SPIDER_ID, required=true,	desc="SPIDER ID") Long spider_id,
			@Param(value=AssetsPriceSpider.SERVICE, required=false,	desc="解析服务") String service
			
		) throws Exception {
		
		return null;
	}

	@MethodInfo("修改")
	public ActionResult updateByAssetsId(
			@Param(value=AssetsPriceSpider.ASSETS_ID, required=true,	desc="投资品种ID", tag="=") Long assets_id,
			@Param(value=AssetsPriceSpider.SERVICE, required=false,	desc="解析服务") String service
		) throws Exception {
		try {
			AssetsPriceSpider spider = this.findByAssetsId(assets_id);
			this.update2(spider.getSpiderId(), service);
		} catch (Exception e) {
			this.create2(assets_id, service);
		}
		return ModelFactory.create(ActionResult.class, Codes.CODE, Codes.SUCCESS);
	}

	@MethodInfo("删除")
	@DaoRemove(entity=AssetsPriceSpider.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=AssetsPriceSpider.SPIDER_ID, required=true,	desc="SPIDER ID") Long spider_id
		) throws Exception {
		
		return null;
	}

	@SuppressWarnings("unchecked")
	@MethodInfo("搜索")
	@DaoQuery(entity=AssetsPriceSpider.ENTITY_NAME, pageable=true, modelClass=AssetsPriceSpider.class)
	public QueryResult<AssetsPriceSpider> findAll(
			@Param(value=AssetsPriceSpider.USER_ID, required=false,	desc="所属用户") Long user_id,
			@Param(value=AssetsPriceSpider.ASSETS_ID, required=false,	desc="投资品种ID") Long assets_id,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) {
		Principal p = ServiceContext.getContext().getPrincipal();
		if(user_id == null && p != null) user_id = Long.parseLong(p.getId());
		return ModelFactory.create(QueryResult.class, 
				AssetsPriceSpider.USER_ID, user_id);
	}

	@MethodInfo("查询")
	@DaoQuery(entity=AssetsPriceSpider.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public AssetsPriceSpider findById(
			@Param(value=AssetsPriceSpider.SPIDER_ID, required=true,	desc="SPIDER ID") Long spider_id
		) throws Exception {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=AssetsPriceSpider.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public AssetsPriceSpider findByAssetsId(
			@Param(value=AssetsPriceSpider.ASSETS_ID, required=true,	desc="投资品种ID", tag="=") Long assets_id
		) throws Exception {
		return null;
	}
	
	//
	public ActionResult fetchById(
			@Param(value=AssetsPriceSpider.SPIDER_ID, required=true,	desc="SPIDER ID") Long spider_id,
			@Param(value="price_date", required=false,	desc="价格日期")Date price_date) throws Exception {
		if(price_date == null) price_date = new Date();
		AssetsPriceSpider spider = this.findById(spider_id);
		IPriceSpider pss = module.getService(spider.getService());
		Assets assets = this.assetsService.findById(spider.getAssetsId());
		SpiderPrice p = pss.fetch(assets.getCode());
		this.assetsService.updatePrice(spider.getAssetsId(), p.getPrice(), price_date);
		return ModelFactory.create(ActionResult.class, Codes.CODE, Codes.SUCCESS);
	}

	@Transaction
	public ActionResult fetchByAssetsId(
			@Param(value=AssetsPriceSpider.ASSETS_ID, required=true,	desc="资产ID") Long assets_id,
			@Param(value="price_date", required=false,	desc="价格日期")Date price_date) throws Exception {
		AssetsPriceSpider spider = this.findByAssetsId(assets_id);
		return this.fetchById(spider.getSpiderId(), price_date);
	}

	public ActionResult fetchAll() {
		List<AssetsPriceSpider> spiders = this.findAll(null, null, 0, Integer.MAX_VALUE).getRows();
		spiders.stream().forEach(x -> {
			try {
				this.fetchById(x.getSpiderId(), new Date());
			} catch (Exception e) {
			}
		});
		return ModelFactory.create(ActionResult.class, Codes.CODE, Codes.SUCCESS);
	}
}